import constant from '@/store/constant.js'
import common from '@/store/common.js'
const baseUrl=constant.baseUrl;
const authInfo=constant.authInfo;

export default {
	state:{//存放状态
		shareItem:{},
		shareList:[],
		
		
		
	},
	mutations: {
		
		loadShareData(state,data){
			
			if(data.flag==1){
				state.shareList=[]
			}
			console.log(state.shareList.length)
			for(let index in data.data){
				state.shareList.push(data.data[index])
			}
			console.log(state.shareList.length)
			
		},
		loadShareInfo(state,data){
			console.log(JSON.stringify(data));
			state.shareItem=data;
		}
		
		
	},
	actions:{
		
		loadShareData(context,data){
			
			console.log("loadShareData"+JSON.stringify(data))
			uni.request({
				url: baseUrl+'/api/note/listForPage',
				method: 'POST',
				header: {
				        'auth': uni.getStorageSync("token") 
				    },
				data: {"pageNum":data.pageNum,"pageSize":data.pageSize},
				success: (res) => {
					if(res.data.code==0){
					
						context.commit('loadShareData',{"data":res.data.result.data,"flag":data.flag})
						
					}else{
						common.dataError(res.data);
						console.log(res.data.message);
					}
			
				},
				fail: (err) => {
					common.jumpToLogin();
					
					console.log(err);
				}
			})
			
		},
		deleteShareData(context,data){
		
					
			uni.request({
				url: baseUrl+'/api/note/deleteNote?noteId='+data,
				method: 'GET',
				header: {
				        'auth': uni.getStorageSync("token") 
				    },
				
				success: (res) => {
					if(res.data.code==0){
						// context.state.shareList=[];
						// context.commit('loadShareData',{"data":res.data.result.data,"flag":data.flag})
						context.dispatch('loadShareData',{"flag":1,"pageNum":1,"pageSize":10})
						
					}else{
						common.dataError(res.data);
						console.log(res.data.message);
					}
			
				},
				fail: (err) => {
					common.jumpToLogin();
					console.log(err);
				}
			})
		},
		topShareData(context,data){
		
			uni.request({
				url: baseUrl+'/api/note/editNote',
				method: 'POST',
				header: {
				        'auth': uni.getStorageSync("token") 
				    },
				data:{"id":data.id,"topFlag":data.topFlag},
				success: (res) => {
					if(res.data.code==0){
						// context.state.shareList=[];
						// context.commit('loadShareData',{"data":res.data.result.data,"flag":data.flag})
						context.dispatch('loadShareData',{"flag":1,"pageNum":1,"pageSize":10})
						
					}else{
						common.dataError(res.data);
						console.log(res.data.message);
					}
			
				},
				fail: (err) => {
					common.jumpToLogin();
					console.log(err);
				}
			})
		},
		addShareData(context,data){
			
			var url="";
			
			if(data.id>0){
				url=baseUrl+"/api/note/editNote";
			}else{
				url=baseUrl+"/api/note/addNote";
			}		
			uni.request({
				url: url,
				method: 'POST',
				header: {
				        'auth': uni.getStorageSync("token") 
				    },
				data:data,
				success: (res) => {
					if(res.data.code==0){
						
						
					}else{
						common.dataError(res.data);
						console.log(res.data.message);
					}
			
				},
				fail: (err) => {
					common.jumpToLogin();
					console.log(err);
				}
			})
		},
		loadShareInfo(context,data){
			
			var url="";
			
				
			uni.request({
				url: baseUrl+"/api/note/getNoteInfo?noteId="+data,
				method: 'POST',
				header: {
				        'auth': uni.getStorageSync("token") 
				    },
				data:data,
				success: (res) => {
					if(res.data.code==0){
						
						context.commit('loadShareInfo',res.data.result)
					}else{
						common.dataError(res.data);
						console.log(res.data.message);
					}
			
				},
				fail: (err) => {
					common.jumpToLogin();
					console.log(err);
				}
			})
		},
		
	}
}